package com.demo.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;


@Slf4j
@Component
public class MybatisFillHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {

        if (metaObject.hasSetter("createTime")) {
            log.info("autoFill createTime");
            this.setFieldValByName("createTime", new Date(), metaObject);
        }

        if (metaObject.hasSetter("isShow")) {
            log.info("autoFill isShow");
            this.setFieldValByName("isShow", 1, metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {

        if (metaObject.hasSetter("updateTime")) {
            log.info("autoFill updateTime");
            this.setFieldValByName("updateTime", new Date(), metaObject);
        }
    }
}
